package site.zhouinfo.servlet05;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用Cookie记录登录信息
 *
 * @author zhou
 * @create 2017-06-28 14:45
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class LoginServletAddCookie extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String nameTransform = new String(name.getBytes("iso8859-1"), "utf-8");
        String password = req.getParameter("pass");
        String saveCookie = req.getParameter("saveCookie");

        System.out.println("从表单中获取到的name: " + name);
        System.out.println("经过处理的name: " + nameTransform);
        System.out.println("从表单中获取到的password: " + password);
        System.out.println("从表单中获取到的saveCookie: " + saveCookie);

        if ("zhou".equals(nameTransform) && "zhou".equals(password)) {
            if ("save".equals(saveCookie)) {
                System.out.println("保存cookie");
                Cookie cookie_name = new Cookie("login_name", nameTransform);
                Cookie cookie_pass = new Cookie("login_pass", password);
                cookie_name.setMaxAge(60 * 60);
                cookie_pass.setMaxAge(60 * 60);
                resp.addCookie(cookie_name);
                resp.addCookie(cookie_pass);
            } else {
                System.out.println("不保存cookie");
                //删除已有的Cookie方法一
                //声明同名的Cookie，将过期时间设置为0，写入到客户端，覆盖掉原来的Cookie
                /*
                Cookie cookie_name = new Cookie("login_name",null);
                Cookie cookie_pass = new Cookie("login_pass",null);
                cookie_name.setMaxAge(0);
                cookie_pass.setMaxAge(0);
                resp.addCookie(cookie_name);
                resp.addCookie(cookie_pass);*/

                //删除已有的Cookie方法二
                //得到本网站所有的Cookie对象，判断是否我们要删除的Cookie
                //是，将过期时间设置为0，写入到客户端
                Cookie[] cookies = req.getCookies();
                for (Cookie cookie : cookies) {
                    if (cookie != null && "login_name".equals(cookie.getName())) {
                        cookie.setMaxAge(0);
                        resp.addCookie(cookie);
                    }
                    if (cookie != null && "login_pass".equals(cookie.getName())) {
                        cookie.setMaxAge(0);
                        resp.addCookie(cookie);
                    }
                }
            }
            req.setAttribute("login_info", nameTransform);
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        } else {
            req.setAttribute("error", "用户名与密码不正确");
            req.getRequestDispatcher("/view/servlet05/login.jsp").forward(req, resp);
        }
    }
}
